/*
 * NotificationWindow.java
 *
 * Created on 7. April 2005, 13:44
 */

/**
 * This Class provides a little borderless window wich is jumping onthe screen to notify a new message.
 * @author  ChrM
 */

package info.chrm.jtalker;

public class NotificationWindow extends javax.swing.JFrame {
    
    private String memberName = "";
    public static final int TOP_LEFT = 0;
    public static final int TOP_RIGHT = 1;
    public static final int BOTTOM_LEFT = 2;
    public static final int BOTTOM_RIGHT = 3;
    
    public static final int WIDTH = 100;
    public static final int HEIGHT = 40;
    
    private AnimationThread at = null;
    int myPosition = BOTTOM_RIGHT;

    private JTalkerClientFront parent = null;    
    
    
    /** Creates new form NotificationWindow */
    public NotificationWindow(JTalkerClientFront _parent) {
        this.parent = _parent;
        initComponents();        
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        jLabelText = new javax.swing.JLabel();
        jButtonOK = new javax.swing.JButton();

        setTitle("New Message");
	setFocusable(false);
	setFocusableWindowState(false);
        setAlwaysOnTop(true);
        setUndecorated(true);
        jLabelText.setBackground(new java.awt.Color(255, 255, 51));
        jLabelText.setFont(new java.awt.Font("Dialog", 0, 10));
        jLabelText.setForeground(new java.awt.Color(0, 0, 0));
        jLabelText.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        jLabelText.setText("You have a new Message!");
        jLabelText.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jLabelText.setOpaque(true);
        getContentPane().add(jLabelText, java.awt.BorderLayout.NORTH);

        jButtonOK.setText("Show");
        jButtonOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonOKActionPerformed(evt);
            }
        });

        getContentPane().add(jButtonOK, java.awt.BorderLayout.CENTER);

        pack();
    }//GEN-END:initComponents

    private void jButtonOKActionPerformed(java.awt.event.ActionEvent evt) {        
        this.parent.toFront();

        if(this.memberName != null && this.memberName != "")
            this.parent.theClient.getMemberByName(this.memberName).showChatWindow(this.parent);

	this.setPosition(this.myPosition);
        this.dispose();
    }
       
    
    public void setVisible(boolean _yes) {
        if(this.isVisible() == _yes)
	   return;

	if(this.at != null && this.at.isRunning())
	   return;

        if(this.at != null) {
            at.stopAnimation();            
        }

        this.at = new AnimationThread(this);        
        this.at.start();
	super.setVisible(_yes);
	this.setPosition(this.myPosition);
    }

    /*public void show() {
       //super.show();
       //this.setVisible(true);
    }*/
    
    public void setMemberName(String _name) {
        if(_name == null) {
            this.memberName = "";
            return;
        }
        this.memberName = _name;
        this.jLabelText.setText(this.memberName);	
    }
    
    public void setPosition(int _pos) {
        this.myPosition = _pos;
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        
        switch(_pos) {
            case NotificationWindow.TOP_LEFT:
                this.setBounds(0, 0, WIDTH, HEIGHT);
                break;
            case NotificationWindow.TOP_RIGHT:
                this.setBounds(screenSize.width - WIDTH, 0, WIDTH, HEIGHT);
                break;
            case NotificationWindow.BOTTOM_LEFT:
                this.setBounds(0, screenSize.height - HEIGHT, WIDTH, HEIGHT);
                break;
            case NotificationWindow.BOTTOM_RIGHT:
                this.setBounds(screenSize.width - WIDTH, screenSize.height - HEIGHT, WIDTH, HEIGHT);
                break;        
        }       
    }
    
    public void dispose() {
        if(at != null)
            this.at.stopAnimation();
        super.dispose();
	//this.setVisible(false);
    }
    
    
    private javax.swing.JButton jButtonOK;
    private javax.swing.JLabel jLabelText;
   
    /** Thread for some gimmicks */
    class AnimationThread extends Thread {
        NotificationWindow parent = null;
	private static final int STEP_SIZE = 1;
	private static final int MOVE_AREA = 50;
        private int stepX = STEP_SIZE;
        private int stepY = STEP_SIZE;
        private boolean isRunning = true;
        
        public AnimationThread(NotificationWindow _parent) {
            this.parent = _parent;    
            
            if(_parent.myPosition == NotificationWindow.TOP_LEFT) {
                stepX *= -1;
                stepY *= -1;
            }
        }
        
        public void stopAnimation() {
            this.isRunning = false;
        }

	public boolean isRunning() {
	   return this.isRunning;
	}
        
        public void run() {
            int runs = MOVE_AREA;
            
            while(runs > 0 && this.isRunning) {
                this.parent.setLocation(this.parent.getLocation().x /* - stepX*/, 
                                        this.parent.getLocation().y - stepY);
                try { 
                        Thread.sleep(100);
                } catch (Exception _e) {} 
                
                runs--;
                
                if(runs == (int)(MOVE_AREA / 2)) {
                    //stepX *= -1;
                    //stepY *= -1;
                    this.isRunning = false;
		    this.parent.setPosition(this.parent.myPosition);                    
                }
            }
           
	    this.isRunning = false;
            this.parent.dispose();
        }
    }
}
